﻿using MoreEvent.Common.Systems;
using MoreEvent.Content.Projectiles;
using Terraria;
using Terraria.GameContent.Bestiary;
using Terraria.GameContent.Personalities;
using Terraria.ID;
using Terraria.Localization;
using Terraria.ModLoader;
using Terraria.Utilities;

namespace MoreEvent.Content.NPCs.Towns
{
    [AutoloadHead]
    public class Chef : ModNPC
    {
        public const string ShopName = "Shop";
        public override void SetStaticDefaults()
        {
            Main.npcFrameCount[Type] = 25;
            NPCID.Sets.ExtraFramesCount[Type] = 9;
            NPCID.Sets.AttackFrameCount[Type] = 4;
            NPCID.Sets.DangerDetectRange[Type] = 700;
            NPCID.Sets.AttackType[Type] = 0;
            NPCID.Sets.AttackTime[Type] = 90;
            NPCID.Sets.AttackAverageChance[Type] = 50;
            NPCID.Sets.HatOffsetY[Type] = 4;
            NPCID.Sets.ShimmerTownTransform[NPC.type] = true;
            NPCID.Sets.ShimmerTownTransform[Type] = true;
            NPCID.Sets.NPCBestiaryDrawModifiers drawModifiers = new()
            {
                Velocity = 1f,
                Direction = -1
            };
            NPCID.Sets.NPCBestiaryDrawOffset.Add(Type, drawModifiers);
            NPC.Happiness
                .SetBiomeAffection<ForestBiome>(AffectionLevel.Like)
                .SetBiomeAffection<JungleBiome>(AffectionLevel.Dislike)
                .SetBiomeAffection<SnowBiome>(AffectionLevel.Love)
                .SetNPCAffection(NPCID.Dryad, AffectionLevel.Love)
                .SetNPCAffection(NPCID.Guide, AffectionLevel.Like)
                .SetNPCAffection(38, AffectionLevel.Dislike)
                .SetNPCAffection(633, AffectionLevel.Hate);
            ContentSamples.NpcBestiaryRarityStars[Type] = 3;
        }
        public override void SetDefaults()
        {
            NPC.townNPC = true;
            NPC.friendly = true;
            NPC.width = 18;
            NPC.height = 40;
            NPC.aiStyle = 7;
            NPC.damage = 10;
            NPC.defense = 15;
            NPC.lifeMax = 250;
            NPC.HitSound = SoundID.NPCHit1;
            NPC.DeathSound = SoundID.NPCDeath1;
            NPC.knockBackResist = 0.5f;
            AnimationType = NPCID.Guide;
        }
        public override void TownNPCAttackStrength(ref int damage, ref float knockback)
        {
            damage = 20;
            knockback = 4f;
        }

        public override void TownNPCAttackCooldown(ref int cooldown, ref int randExtraCooldown)
        {
            cooldown = 30;
            randExtraCooldown = 30;
        }

        public override void TownNPCAttackProj(ref int projType, ref int attackDelay)
        {
            projType = ModContent.ProjectileType<FriedChickenBoomerang>();
            attackDelay = 1;
        }

        public override void TownNPCAttackProjSpeed(ref float multiplier, ref float gravityCorrection, ref float randomOffset)
        {
            multiplier = 12f;
            randomOffset = 2f;
        }
        public override void SetBestiary(BestiaryDatabase database, BestiaryEntry bestiaryEntry)
        {
            bestiaryEntry.Info.AddRange([
                BestiaryDatabaseNPCsPopulator.CommonTags.SpawnConditions.Biomes.Surface,
                new FlavorTextBestiaryInfoElement("Mods.MoreEvent.Bestiary.Chef"),
            ]);
        }
        public override string GetChat()
        {
            WeightedRandom<string> chat = new();
            chat.Add(Language.GetTextValue("Mods.MoreEvent.Dialogue.Chef.1"));
            chat.Add(Language.GetTextValue("Mods.MoreEvent.Dialogue.Chef.2"));
            chat.Add(Language.GetTextValue("Mods.MoreEvent.Dialogue.Chef.3"));
            chat.Add(Language.GetTextValue("Mods.MoreEvent.Dialogue.Chef.4"));
            chat.Add(Language.GetTextValue("Mods.MoreEvent.Dialogue.Chef.5"));
            chat.Add(Language.GetTextValue("Mods.MoreEvent.Dialogue.Chef.6"));
            chat.Add(Language.GetTextValue("Mods.MoreEvent.Dialogue.Chef.7"));
            return chat.Get();
        }
        public override void SetChatButtons(ref string button, ref string button2)
        {
            button = Language.GetTextValue("LegacyInterface.28");
        }
        public override void OnChatButtonClicked(bool firstButton, ref string shop)
        {
            if (firstButton)
            {
                shop = ShopName;
            }
        }
        public override void AddShops()
        {
            var npcShop = new NPCShop(Type, ShopName)
            .Add(new Item(ModContent.ItemType<Items.CrazyThursday.FriedChickenBoomerang>()))
            .Add(new Item(ModContent.ItemType<Items.CrazyThursday.PlainChickenNuggets>()))
            .Add(new Item(4021))
            .Add(new Item(4015))
            .Add(new Item(ModContent.ItemType<Items.CrazyThursday.LegendaryFamilyMeal>()),Condition.DownedPlantera);
            npcShop.Register();
        }
        public override bool CanTownNPCSpawn(int numTownNPCs)
        {
            if (EventSystem.ActiveEvents.Contains(2))
            {
                return true;
            }
            return false;
        }
        public override void HitEffect(NPC.HitInfo hit)
        {
            int num = NPC.life > 0 ? 1 : 5;

            for (int k = 0; k < num; k++)
            {
                Dust.NewDust(NPC.position, NPC.width, NPC.height, DustID.Blood);
            }
        }
    }
}
